package com.softserveinc.ita.kaiji.web.rest;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.softserveinc.ita.kaiji.dto.GameInfoDto;
import com.softserveinc.ita.kaiji.model.player.bot.Bot;
import com.softserveinc.ita.kaiji.service.GameService;



/**
 * Controller for REST new game creation
 * @author Boiko Eduard
 * @version 1.1
 * @since 1.04.14 
 */


@Controller
@Scope("session")
@RequestMapping(value="/rest/new", produces="application/json")
public class RestCreateGameController {
	
    private static Logger logger = Logger.getLogger(RestCreateGameController.class);

    @Autowired
    private GameService gameService;
    
    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public String createRestGameWithDefaultSettings(HttpSession session){
        
    	GameInfoDto gameInfoDto = new GameInfoDto();
    	gameInfoDto.setGameName("Training");
    	gameInfoDto.setPlayerName("Batman");
    	gameInfoDto.setNumberOfCards(3);
    	gameInfoDto.setBotType(Bot.Types.MEDIUM);
        Integer gameId   = gameService.createGame(gameInfoDto);
        Integer playerId = gameService.getPlayerIdFromGame(gameId);
        session.setAttribute("playerId", playerId);
        
		if (logger.isTraceEnabled()){
			logger.trace("Default RestGame created. Session attr. playerId set");}
		
        return "Game created. You can proceed to kaiji/rest/game/" + gameId + "/";
        
    }
    
    //Example of GameInfoDto json, which can be put in POST-request
    //{"gameName":"Training","playerName":"Batman","isBotGame":true,"numberOfCards":3,"botType":"HARD"}

    @RequestMapping(method = RequestMethod.POST, consumes="application/json")
    @ResponseBody
    public String createRestGameWithCustomSettings(@RequestBody GameInfoDto gameInfoDto, HttpSession session){

        Integer gameId   = gameService.createGame(gameInfoDto);
        Integer playerId = gameService.getPlayerIdFromGame(gameId);
        session.setAttribute("playerId", playerId);
        
		if (logger.isTraceEnabled()){
			logger.trace("Custom RestGame created. Session attr. playerId set");}
		
        return "Game created. You can proceed to kaiji/rest/game/" + gameId + "/";
    }
    
}
